package com.newangels.agent;

import jade.core.AID;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;
import jade.util.leap.ArrayList;
import jade.util.leap.List;
import jade.wrapper.gateway.GatewayAgent;
import jade.wrapper.gateway.JadeGateway;

/**
 * 用于关闭Agent的中间件
 *
 * @author: TangLiang
 * @date: 2020/5/17 16:18
 * @since: 1.0
 */
public class KillAgentGatewayAgent extends GatewayAgent {

    private List agentList = new ArrayList();

    @Override
    protected void processCommand(Object command) {
        if (command instanceof List) {
            agentList = (ArrayList) command;
            addBehaviour(new OneShotBehaviour(this) {
                @Override
                public void action() {
                    ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
                    if ((agentList != null)) {
                        releaseCommand(agentList);
                        for (int i = 0, len = agentList.size(); i < len; i++) {
                            String a = (String) agentList.get(i);
                            msg.addReceiver(new AID(a, AID.ISLOCALNAME));
                        }
                        msg.setContent("killAgent");
                        msg.setConversationId("AgentManageFromMyGatewayAgent");
                        myAgent.send(msg);
                        agentList.clear();
                    }
                }
            });
        }
    }

    @Override
    protected void setup() {
        super.setup();
    }
}
